//
//  ZHLoginViewController.m
//  ZHS_UCDemo
//
//  Created by Emrys on 2016/11/7.
//  Copyright © 2016年 Emrys. All rights reserved.
//

#import "ZHLoginViewController.h"

#import "ZHButton.h"
#import "ZHTextField.h"

#import "ZHTools.h"
#import "ZHRequestManager.h"

#import "ZHS_UC_API.h"

#import "ZHLoginModel.h"

#import "ZHDrawerController.h"

static const NSString *kZHLoginNameKey = @"loginName";
static const NSString *kZHPasswordKey = @"password";

@interface ZHLoginViewController ()

@property (weak, nonatomic) IBOutlet ZHTextField *userNameTextField;
@property (weak, nonatomic) IBOutlet ZHTextField *passwordTextField;

@property (weak, nonatomic) IBOutlet ZHButton *loginButton;

@property(nonatomic, strong, readonly) ZHRequestManager *requestManager;

@property(nonatomic, strong) ZHLoginModel *loginModel;

@end

@implementation ZHLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.userNameTextField.text = @"12345678903";
    self.passwordTextField.text = @"12345678";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (IBAction)loginButtonDidClick:(ZHButton *)sender {
    
    [ZHTools storeUpIsDemo:NO];
    
    NSDictionary *paramters = @{
                                kZHLoginNameKey:self.userNameTextField.text
                                ,kZHPasswordKey:[ZHTools MD5WithString:[ZHTools MD5WithString:self.passwordTextField.text]]
                                };

    
    [self loginWithParamters:paramters];
}
- (IBAction)demoButtonDidClick:(ZHButton *)sender {
    
    [ZHTools storeUpIsDemo:YES];
    
    NSDictionary *paramters = @{
                                kZHLoginNameKey:self.userNameTextField.text
                                ,kZHPasswordKey:[ZHTools MD5WithString:[ZHTools MD5WithString:self.passwordTextField.text]]
                                };

    
    [self loginWithParamters:paramters];
}

#pragma mark - method
- (void)loginWithParamters:(NSDictionary *)paramters {

//    if ([ZHTools isDemo]) {
//        [self requestWithParamters:paramters];
//    } else {
//        [self requestCloudSeaWithParamters:paramters];
//    }
    [self requestWithParamters:paramters];
}

- (void)hasLoggedin {
    
    [UIApplication sharedApplication].keyWindow.rootViewController
        = [ZHDrawerController drawerControllerWithLeaderSymbol:[ZHTools leaderSymbol]];
}

#pragma mark - request
- (void)requestWithParamters:(NSDictionary *)paramters {
    [self.requestManager loginRequest:(NSString *)kZHS_LOGIN httpMethod:ZH_POST parameters:paramters handle:^(id object, NSError *error) {
        if (object) {
            
            NSLog(@"%@", object);
            self.loginModel = [ZHLoginModel makeModelWithObject:object];
            
            [ZHTools storeUpLeaderWithLeaderSymbol:self.loginModel.isLeader];
//            [ZHTools storeUpLeaderWithLeaderSymbol:1];
            
            
            
            [self hasLoggedin];
            
        } else {
            [self showTextHint:[error.userInfo valueForKey:@"content"]];
        }
    }];
}
- (void)requestCloudSeaWithParamters:(NSDictionary *)paramters {
    
    NSLog(@"暂不支持云海！");
    
    [self showTextHint:[NSString stringWithFormat:@"暂不支持云海！ %@",[NSString localizableStringWithKey:@"key"]]];
}

#pragma mark -
#pragma mark - property
- (ZHRequestManager *)requestManager{
    return [ZHRequestManager sharedManager];
}
@end
